<template>
  <div>
    <!-- 顶部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <!-- 添加分类按钮 -->
          <el-button type="primary" @click="showCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template v-slot:order="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-if="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template v-slot:opt="scope">
          <!-- 编辑分类 -->
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editCateInfo(scope.row.cat_name, scope.row.cat_id)">编辑</el-button>
          <!-- 删除分类 -->
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="delCateInfo(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        class="el-page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类弹框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      :before-close="closeDialog"
    >
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <el-form label-width="100px" class="demo-ruleForm">
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            v-model="parentCateKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
          >
        </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogClosed">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类弹窗 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      //* 编辑分类
      editForm: {
        cat_name: '',
        cat_id: ''
      },
      editFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      addCateDialogVisible: false,
      // 商品分类列表
      catelist: [],
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          // *当前列定义为模版列
          type: 'template',
          template: 'isok'
        }, {
          label: '排序',
          // *当前列定义为模版列
          type: 'template',
          template: 'order'
        }, {
          label: '操作',
          // *当前列定义为模版列
          type: 'template',
          template: 'opt'
        }
      ],
      // *分类表单数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // *分类表单数据对象规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      //* 父级数据分类列表
      parentCateList: [],
      cascaderProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //* 选中的父级分类的ID数组
      parentCateKeys: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // *获取商品分类数据
    getCateList () {
      this.$http.getCateList(this.querInfo)
        .then(resp => {
          this.catelist = resp.data.data.result
          this.total = resp.data.data.total
          // console.log(resp.data.data)
        })
    },
    // *监听pagessize改变
    handleSizeChange (newsize) {
      this.querInfo.pagesize = newsize
      this.getCateList()
    },
    // *监听pagenum的改变
    handleCurrentChange (newpage) {
      this.querInfo.pagenum = newpage
      this.getCateList()
    },
    // *点击展示添加分类对话框
    showCate () {
      this.querInfo.type = 2
      this.querInfo.pagenum = null
      this.querInfo.pagesize = null
      // *获取父级数据分类
      this.$http.getCateList(this.querInfo)
        .then(resp => {
          // console.log(resp)
          const { data: res } = resp
          this.parentCateList = res.data
          // console.log(this.parentCateList)
        })
      this.addCateDialogVisible = true
    },
    //* 选择项发生变化触发这个事件
    parentCateChange () {
      // console.log(this.parentCateKeys)
      const keylength = this.parentCateKeys.length
      if (keylength > 0) {
        this.addCateForm.cat_pid = this.parentCateKeys[keylength - 1]
        this.addCateForm.cat_level = keylength
        // console.log(this.addCateForm)
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    //* 确定添加分类事件
    addCate () {
      // console.log(this.addCateForm)
      // addCateDialogVisible = false
      this.$refs.addCateFormRef.validate(valid => {
        if (!valid) return
        this.$http.addGoodCate(this.addCateForm)
          .then(resp => {
            // console.log(resp.data)
            if (resp.data.meta.status !== 201) {
              this.$message.error(resp.data.meta.msg)
            } else {
              this.$message.success('添加成功')
              this.addCateDialogVisible = false
              this.querInfo = {
                type: 3,
                pagenum: 1,
                pagesize: 5
              }
              this.getCateList()
            }
          })
      })
    },
    //* 取消添加分类
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.parentCateKeys = []
      this.addCateDialogVisible = false
      // console.log(this.addCateForm)
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    //* 弹框右上角X
    closeDialog () {
      this.addCateDialogVisible = false
      this.addCateDialogClosed()
    },
    //* 编辑分类名称
    editCateInfo (name, id) {
      this.editDialogVisible = true
      this.editForm.cat_name = name
      this.editForm.cat_id = id
      // console.log(name, id)
    },
    //* 编辑分类名称弹窗确定提交事件
    editAddForm () {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        this.$http.putGoodsCategories(this.editForm.cat_id, this.editForm.cat_name)
          .then(
            this.$message.success('更改成功')
          )
        this.editDialogVisible = false
      })
      this.getCateList()
    },
    //* 删除分类
    delCateInfo (id) {
      this.$confirm('此操作将删除此商品分类, 是否继续?', '删除角色', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.deleteGoodscategorie(id)
          .then(resp => {
            this.getCateList()
          })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // console.log(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-card {
    margin-top: 15px;
    .el-row {margin-bottom: 15px;}
    .el-page {margin-top: 15px;}
  }
  .el-cascader {width: 100%;}
</style>
